//
//  YunDanCenterDetailViewController.m
//  阿帕
//
//  Created 阿帕 on 2019/12/27.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "YunDanCenterDetailViewController.h"
#import "YunDanTrackViewController.h"
#import "YunDanCenterExtraFeeViewController.h"
#import "YunDanCheckPhotoViewController.h"
#import "CYWebViewViewController.h"

#import "YunDanDetailHeaderView.h"
#import "YunDanDetailIconHeader.h"
#import "YunDanCenterTabbar.h"

#import "YDCenterDetailCell.h"
#import "PaiDanViewController.h"

@interface YunDanCenterDetailViewController ()<UITableViewDataSource,
UITableViewDelegate, YunDanCenterDetailHeaderViewDelegate>
{
    BOOL _isOpenYuFuAmount;
    BOOL _isOpenShiShouCashAmount;
}
@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSDictionary *dataDic;

@property (strong , nonatomic)NSMutableArray *huoWuTitleArray;
@property (strong , nonatomic)NSMutableArray *huoWuValueArray;
@property (strong , nonatomic)NSMutableArray *feiYongTitleArray;
@property (strong , nonatomic)NSMutableArray *feiYongValueArray;
@property (strong , nonatomic)NSMutableArray *otherTitleArray;
@property (strong , nonatomic)NSMutableArray *otherValueArray;

@end

@implementation YunDanCenterDetailViewController

static const CGFloat cellHeight = 35;

static const CGFloat headerHeight = 465;

static const CGFloat headerHeightTwo = 40;

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self layoutBaseView];
}

- (void)layoutBaseView {
    
    self.navigationItem.title = @"运单详情";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self detailInfoRequest];
}

- (void)detailInfoRequest {
    WEAKSELF
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], ORDER_DETAIL, _orderCode) withParameter:@{@"token":[UserDefaultUtil valueForKey:USER_TOKEN],} withVC:self netName:@"运单中心详情" success:^(id response, NSString *msg) {
        
        _dataDic = response;
        
        NSString *hlshString = @"";
        if ([_dataDic[@"order"][@"limitWastageAmount"] integerValue] == 0) {
            hlshString = @"不计";
        } else {
            hlshString = CJStringWithFormat(@"%@%@", _dataDic[@"order"][@"limitWastageAmount"], _dataDic[@"order"][@"limitWastageTypeName"]);
        }
        
        /************       货物信息        ************/
        NSArray *valueArr = @[
            [NSString nullToString:CJStringWithFormat(@"%@",_dataDic[@"order"][@"coalTypeName"])],
            [NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"order"][@"coalPrice"] floatValue])],
            [NSString stringWithFormat:@"%@%@", [DCSpeedy isPureInt:CJStringWithFormat(@"%@", _dataDic[@"orderDetail"][@"loadQuantity"])] ? _dataDic[@"orderDetail"][@"loadQuantity"] : [NSString nullToString:CJStringWithFormat(@"%.3f",[_dataDic[@"orderDetail"][@"loadQuantity"] floatValue])], [NSString nullToString:CJStringWithFormat(@"%@", _dataDic[@"order"][@"coalUnitName"])]],
            
            [NSString stringWithFormat:@"%@%@", [DCSpeedy isPureInt:CJStringWithFormat(@"%@",_dataDic[@"orderDetail"][@"unloadQuantity"])] ? _dataDic[@"orderDetail"][@"unloadQuantity"] : [NSString nullToString:CJStringWithFormat(@"%.3f",[_dataDic[@"orderDetail"][@"unloadQuantity"] floatValue])], [NSString nullToString:CJStringWithFormat(@"%@", _dataDic[@"order"][@"coalUnitName"])]],
            
//            [DCSpeedy isBlankString: CJStringWithFormat(@"%ld", (long)[_dataDic[@"order"][@"limitWastageAmount"] integerValue])] == 0 ? @"不计" : CJStringWithFormat(@"%@%@", _dataDic[@"order"][@"limitWastageAmount"], _dataDic[@"order"][@"limitWastageTypeName"]),
            hlshString,
            CJStringWithFormat(@"%.2f公里", [_dataDic[@"order"][@"distance"] floatValue]),
            [NSString stringWithFormat:@"%@", _dataDic[@"order"][@"remark"] ? _dataDic[@"order"][@"remark"] : @"暂无"] 

        ];
        
        [weakSelf.huoWuValueArray addObjectsFromArray:valueArr];
       
        
        /******************************** 费用信息  ******************************/
        
        NSInteger status = [_dataDic[@"orderDetail"][@"status"] integerValue];//订单状态
        if ([_dataDic[@"order"][@"fareInfoDisplay"] intValue] == 1) {
            [weakSelf.feiYongTitleArray addObject:@"承运单价"];
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"realPrice"] floatValue])]];//承运单价
        }
       
        
        if (status >= 3) {
            //结算吨位
            NSString *settlementWeight = @"0.000吨";
            if (status == 1 || status == 2) {
                //待运输、运输中成两个状态运单 结算吨位 为0.000吨
                settlementWeight = @"0.000吨";
            } else {
                settlementWeight = [DCSpeedy isPureInt:CJStringWithFormat(@"%@", response[@"orderDetail"][@"settlementWeight"])] ? [NSString nullToString:CJStringWithFormat(@"%@吨", response[@"orderDetail"][@"settlementWeight"])] : [NSString nullToString:CJStringWithFormat(@"%.3f吨", [response[@"orderDetail"][@"settlementWeight"] floatValue])];
            }
            
            [weakSelf.feiYongTitleArray addObject:@"结算吨位"];
            [weakSelf.feiYongValueArray addObject:settlementWeight];
        }
        
        if (status >= 4) {
            [weakSelf.feiYongTitleArray addObject:@"异常费用"];
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"abnormalFee"] floatValue])]];//异常费用
        }
        
        if (status >= 3 && [_dataDic[@"orderDetail"][@"isUnloadingConfirm"] integerValue] == 1) {
            //已结算 其他费用
            [weakSelf.feiYongTitleArray addObject:@"其他费用"];
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元  >>", [_dataDic[@"orderDetail"][@"extraFee"] floatValue])]];
        }
        if ([_dataDic[@"order"][@"fareInfoDisplay"] intValue] == 1) {
            [weakSelf.feiYongTitleArray addObject:@"预估运费"];
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"deliveryFeeDeserved"] floatValue])]];//预估运费
        }
        
        if ([_dataDic[@"orderDetail"][@"payTaxAmount"] floatValue] > 0.00) {
            //预付金额 >0 时展示
            [weakSelf.feiYongTitleArray addObject:@"预付金额"];
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元 >>", [_dataDic[@"orderDetail"][@"payTaxAmount"] floatValue])]];
        }
        if (status >= 3) {
            [weakSelf.feiYongTitleArray addObject:@"应收运费"];
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"deliveryFeePractical"] floatValue])]];//应收运费
        }
        
        if (status >= 4) {
            //已结算的展示 - 实付运费、实付现金、合计收款、异常费用
            [weakSelf.feiYongTitleArray addObject:@"实收运费"];
            [weakSelf.feiYongTitleArray addObject:@"实收现金"];
            [weakSelf.feiYongTitleArray addObject:@"合计收款"];
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"driverActualFreight"] floatValue])]];//实收运费
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元 >>", [_dataDic[@"orderDetail"][@"driverActualCash"] floatValue])]];//实收现金
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"deliveryCashFee"] floatValue])]];//合计收款
            
        }
        

        if ([_dataDic[@"orderDetail"][@"isInsure"] integerValue] == 1) {
            //已投保
            [weakSelf.feiYongTitleArray addObject:@"投保金额"];
            [weakSelf.feiYongValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%.2f元/车", [_dataDic[@"orderDetail"][@"insuranceFee"] floatValue])]];
        }
         
        if (status == 5) {
            //已打款
//            [weakSelf.feiYongTitleArray addObject:@"实得运费"];
//            [weakSelf.feiYongTitleArray addObject:@"已分配现金"];
//            [weakSelf.feiYongTitleArray addObject:@"已分配油点"];
//            [weakSelf.feiYongValueArray addObject:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"deliveryFeePractical"] floatValue])];
//            [weakSelf.feiYongValueArray addObject:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"allotAmount"] floatValue])];
//            [weakSelf.feiYongValueArray addObject:CJStringWithFormat(@"%@",_dataDic[@"orderDetail"][@"allotOil"])];
        }
        
        
        
        /******************************** 其他信息  ******************************/
        
        [weakSelf.otherValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%@",_dataDic[@"orderDetail"][@"gmtCreated"])]];
        [weakSelf.otherValueArray addObject:[DCSpeedy isBlankString:_dataDic[@"orderDetail"][@"fillTime"]] == YES ? @"暂无" : _dataDic[@"orderDetail"][@"fillTime"]];
        [weakSelf.otherValueArray addObject:[DCSpeedy isBlankString:_dataDic[@"orderDetail"][@"signTime"]] == YES ? @"暂无" : _dataDic[@"orderDetail"][@"signTime"]];
        if (status >= 4) {
            //已结算
            [weakSelf.otherTitleArray insertObject:@"核算时间" atIndex:3];
            [weakSelf.otherValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%@",_dataDic[@"orderDetail"][@"settleTime"])]];
        }
        [weakSelf.otherValueArray addObject:@"干线运输"];
        [weakSelf.otherValueArray addObject:[DCSpeedy isBlankString:_dataDic[@"orderDetail"][@"coalName"]] == YES ? @"暂无" : _dataDic[@"orderDetail"][@"coalName"]];
        
        if (status == 5) {
            //已打款
            [weakSelf.otherTitleArray addObject:@"打款时间"];
            [weakSelf.otherValueArray addObject:[NSString nullToString:CJStringWithFormat(@"%@",_dataDic[@"orderDetail"][@"payTime"])]];
            
        }
        
        
        [weakSelf.view addSubview:self.tableView];
        
        //        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

#pragma mark - YunDanCenterDetailHeaderViewDelegate
- (void)jiedanBtnClickEvent:(NSInteger)takeOrderType vehicleCode:(NSString *)vehicleCode {
    if (takeOrderType == 1) {
        NSString *url = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], @"/orderDetailSingle/singleOrderDetail");
        NSDictionary *dicTemp = @{
            @"code": StringCheck(self.orderCode),
            @"driverCode":StringCheck([UserDefaultUtil valueForKey:UserPartyCode])};
        @weakify(self);
        [HTTPManager postNetworkRequest:url
                          withParameter:dicTemp
                                 withVC:self
                                netName:@"司机扫码车队长接单的二维码后自动接单"
                                success:^(id response, NSString *msg) {
            @strongify(self);
            YunDanCenterTabbar *vc = [YunDanCenterTabbar new];
            vc.selectedIndex = 2;
            [self.navigationController pushViewController:vc animated:YES];
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
        
    } else if (takeOrderType == 2) {
        
        NSString *url = CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], @"/teamOrder/receive");
        NSDictionary *dicTemp = @{
            @"vehicleCode" : StringCheck(vehicleCode),
            @"code": StringCheck(self.orderCode),
        };
        @weakify(self);
        [HTTPManager postNetworkRequest:url
                          withParameter:dicTemp
                                 withVC:self
                                netName:@"司机扫码车队长接单的二维码后自动接单"
                                success:^(id response, NSString *msg) {
            @strongify(self);
            YunDanCenterTabbar *vc = [YunDanCenterTabbar new];
            vc.selectedIndex = 2;
            [self.navigationController pushViewController:vc animated:YES];
            
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
    }
}

- (void)tableHeaderViewClickEvent:(NSInteger)index andTitle:(nonnull NSString *)title{
    if ([title isEqual:@"装车单"]) {
        //装车单
        NSArray *fillImageInfoList = _dataDic[@"fillImageInfoList"];
        NSMutableArray *muArr = [NSMutableArray array];
        [fillImageInfoList enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([DCSpeedy isBlankString:obj] == NO) {
                [muArr addObject:obj];
            }
        }];
        if ([DCSpeedy isBlankArr:muArr] == NO) {
            YunDanCheckPhotoViewController *ydcpvc = [[YunDanCheckPhotoViewController alloc] init];
            ydcpvc.photosArray = muArr;
            ydcpvc.navTitle = @"装车磅单";
            [self.navigationController pushViewController:ydcpvc animated:YES];
        } else {
            [XSInfoView showInfo:@"暂无装车磅单" onView:self.view];
        }
        
    } else if ([title isEqual:@"卸车磅单"]) {
        //卸车单
        NSArray *signImageInfoList = _dataDic[@"signImageInfoList"];
        NSMutableArray *muArr = [NSMutableArray array];
        [signImageInfoList enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([DCSpeedy isBlankString:obj] == NO) {
                [muArr addObject:obj];
            }
        }];
        if ([DCSpeedy isBlankArr:muArr] == NO) {
            YunDanCheckPhotoViewController *ydcpvc = [[YunDanCheckPhotoViewController alloc] init];
            ydcpvc.photosArray = muArr;
            ydcpvc.navTitle = @"卸车磅单";
            [self.navigationController pushViewController:ydcpvc animated:YES];
        } else {
            [XSInfoView showInfo:@"暂无卸车磅单" onView:self.view];
        }
        
    } else if ([title isEqual:@"运输单轨迹"]) {
        //运输轨迹单
        if ([DCSpeedy isBlankArr:_dataDic[@"orderTraceList"]] == NO) {
            YunDanTrackViewController *ydtvc = [[YunDanTrackViewController alloc] init];
            NSMutableArray *arr = [NSMutableArray array];
            [arr addObjectsFromArray:[YunDanTrackRecordModel mj_objectArrayWithKeyValuesArray:_dataDic[@"orderTraceList"]]];
            ydtvc.trackArray = arr;
            [self.navigationController pushViewController:ydtvc animated:YES];
        } else {
            [XSInfoView showInfo:@"暂无运单轨迹" onView:self.view];
        }
    } else if ([title containsString:@"查看路线"]) {
        
        //查看路线
        NSString *shipperLongitude = [NSString nullToString:_dataDic[@"order"][@"shipperLongitude"]];//装货地经度
        NSString *shipperLatitude = [NSString nullToString:_dataDic[@"order"][@"shipperLatitude"]];//装货地纬度
        NSString *shipperAddress = [NSString nullToString:_dataDic[@"order"][@"shipperAddress"]];//装货地地址
        
        NSString *consigneeLongitude = [NSString nullToString:_dataDic[@"order"][@"consigneeLongitude"]];
        NSString *consigneeLatitude = [NSString nullToString:_dataDic[@"order"][@"consigneeLatitude"]];
        NSString *consigneeAddress = [NSString nullToString:_dataDic[@"order"][@"consigneeAddress"]];
        
        NSDictionary *dic = @{
            @"shipperLongitude": shipperLongitude,
            @"shipperLatitude": shipperLatitude,
            @"shipperAddress": shipperAddress,
            @"consigneeLongitude": consigneeLongitude,
            @"consigneeLatitude": consigneeLatitude,
            @"consigneeAddress": consigneeAddress,
        };
        CYWebViewViewController *webVC = [[CYWebViewViewController alloc] init];
        webVC.titleString = @"查看路线";
        webVC.urlString = CJStringWithFormat(@"%@/admin/#/enclosure?orderCode=%@", BasicDomainURL, _dataDic[@"order"][@"code"]);
        webVC.state = 1;
        webVC.navDic = dic;
        [self.navigationController pushViewController:webVC animated:YES];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    } else if (section == 1) {
        //货物信息
        return self.huoWuTitleArray.count;
    } else if (section == 2) {
        //费用信息
        if(![DCSpeedy isBlankString:_dataDic[@"orderDetail"][@"teamName"]] && [[UserDefaultUtil valueForKey:DRIVER_TYPE] integerValue] != 1){
            return 0;
        }else{
            return self.feiYongTitleArray.count;
        }
        
        
    } else {
        //其他信息
        return self.otherTitleArray.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YDCenterDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 1) {
        
        cell.titleLabel.text = self.huoWuTitleArray[indexPath.row];
        cell.valueLabel.text = self.huoWuValueArray[indexPath.row];
        
    } else if (indexPath.section == 2) {
        if(![DCSpeedy isBlankString:_dataDic[@"orderDetail"][@"teamName"]] && [[UserDefaultUtil valueForKey:DRIVER_TYPE] integerValue] != 1){
            
        }else{
            cell.titleLabel.text = self.feiYongTitleArray[indexPath.row];
            cell.valueLabel.text = self.feiYongValueArray[indexPath.row];
            if ([cell.titleLabel.text isEqual:@"预付现金"] || [cell.titleLabel.text isEqual:@"预付油点"] || [cell.titleLabel.text isEqual:@"打款现金"] || [cell.titleLabel.text isEqual:@"打款油点"]) {
                cell.titleLabel.font = font(12);
                cell.valueLabel.font = font(12);
                cell.titleLabLeading.constant = 20;
                cell.valueLabTrailing.constant = 20;
            } else {
                cell.titleLabel.font = font(13);
                cell.valueLabel.font = font(13);
                cell.titleLabLeading.constant = 15;
                cell.valueLabTrailing.constant = 15;
            }
        }
        
        
    } else if (indexPath.section == 3) {
        
        cell.titleLabel.text = self.otherTitleArray[indexPath.row];
        cell.valueLabel.text = self.otherValueArray[indexPath.row];
        
    }
    //    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        float width = (ScreenW-30-30)/3;
        NSArray *fillImageInfoList = _dataDic[@"fillImageInfoList"];
        NSArray * signImageInfoList = _dataDic[@"signImageInfoList"];
        NSMutableArray *muArr = [NSMutableArray array];
        [fillImageInfoList enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([DCSpeedy isBlankString:obj] == NO) {
                [muArr addObject:obj];
            }
        }];
        NSMutableArray *muArr1 = [NSMutableArray array];
        [signImageInfoList enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([DCSpeedy isBlankString:obj] == NO) {
                [muArr1 addObject:obj];
            }
        }];
        float a = 0;
        float b = 0;
        if (muArr.count == 0) {
            a = ((float)width * ((int)muArr.count / 3));
        }else{
            a = ((float)width * ((int)muArr.count / 3 + 1));
        }
        if (muArr1.count == 0) {
            b = ((float)width * ((int)muArr1.count / 3));
        }else{
            b = ((float)width * ((int)muArr1.count / 3 + 1));
        }
        if (_takeOrderType == 1) {
            
//            NSArray * arr = _dataDic[@"orderDetail"][@"signImageInfoList"];
            return headerHeight + 60 + a + b;
        } else if (_takeOrderType == 2) {
            return headerHeight + 100 + a + b;
        }else{
            return headerHeight+ a + b;
        }
        return headerHeight ;
    } else if (section == 2){
        if(![DCSpeedy isBlankString:_dataDic[@"orderDetail"][@"teamName"]] && [[UserDefaultUtil valueForKey:DRIVER_TYPE] integerValue] != 1){
            return 0;
        }else{
            return headerHeightTwo;
        }
    }else{
        return headerHeightTwo;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        NSString *status = CJStringWithFormat(@"%ld",(long)_ydModel.status);
        if ([_assignmentStatus isEqual:@"waitCar"] ||
            ([_assignmentStatus isEqual:@"all"] && [DCSpeedy isBlankString:_ydModel.driverCode])) {
            if (Identity_CarCaptain) {
                //独立司机加入车队或车队司机
                return 60;
            }
        } else {
            
            if ([status isEqual:@"1"]) {
                //0独立司机 1车队 2车队司机
                if ([CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]) isEqual:@"1"] && [DCSpeedy isBlankString:_ydModel.driverCode] == YES && _ydModel.isCancel != 1) {
                    //派单按钮显示
                    return 60;
                }
            } else if ([status isEqual:@"2"]) {
                //0独立司机 1车队 2车队司机
                if ([CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]) isEqual:@"1"] && [DCSpeedy isBlankString:_ydModel.driverCode]) {
                    //派单按钮显示
                    return 60;
                }
            }
        }
    
    }
    return 0.0001;
   
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 50)];
    view.backgroundColor = UIColor.whiteColor;
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setTitle:@"派单" forState:(UIControlStateNormal)];
    [btn setBackgroundColor:Theme_ColorValue];
    [btn setFrame:CGRectMake(15, 5,ScreenW - 30 , 50)];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 10;
    [btn addTarget:self action:@selector(paidanAction) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:btn];
    if (section == 3) {
        NSString *status = CJStringWithFormat(@"%ld",(long)_ydModel.status);
        if ([_assignmentStatus isEqual:@"waitCar"] ||
            ([_assignmentStatus isEqual:@"all"] && [DCSpeedy isBlankString:_ydModel.driverCode])) {
            if (Identity_CarCaptain) {
                //独立司机加入车队或车队司机
                return view;
            }
        } else {
            
            if ([status isEqual:@"1"]) {
                //0独立司机 1车队 2车队司机
                if ([CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]) isEqual:@"1"] && [DCSpeedy isBlankString:_ydModel.driverCode] == YES && _ydModel.isCancel != 1) {
                    //派单按钮显示
                    return view;
                }
            } else if ([status isEqual:@"2"]) {
                //0独立司机 1车队 2车队司机
                if ([CJStringWithFormat(@"%@",[UserDefaultUtil valueForKey:DRIVER_TYPE]) isEqual:@"1"] && [DCSpeedy isBlankString:_ydModel.driverCode]) {
                    //派单按钮显示
                    return view;
                }
            }
        }
    
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        float width = (ScreenW-30-30)/3;
        NSArray * signImageInfoList = _dataDic[@"signImageInfoList"];
        NSArray *fillImageInfoList = _dataDic[@"fillImageInfoList"];
        NSMutableArray *muArr = [NSMutableArray array];
        [fillImageInfoList enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([DCSpeedy isBlankString:obj] == NO) {
                [muArr addObject:obj];
            }
        }];
        NSMutableArray *muArr1 = [NSMutableArray array];
        [signImageInfoList enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([DCSpeedy isBlankString:obj] == NO) {
                [muArr1 addObject:obj];
            }
        }];
        CGFloat height = headerHeight;
        float a = 0;
        float b = 0;
        if (muArr.count == 0) {
            a = ((float)width * ((int)muArr.count / 3));
        }else{
            a = ((float)width * ((int)muArr.count / 3 + 1));
        }
        if (muArr1.count == 0) {
            b = ((float)width * ((int)muArr1.count / 3));
        }else{
            b = ((float)width * ((int)muArr1.count / 3 + 1));
        }
        if (_takeOrderType == 1) {
            height = headerHeight + 60 + a + b;
        } else if (_takeOrderType == 2) {
            height = headerHeight + 100 + a  + b;
        }else{
            height = headerHeight  + a  + b;
        }
        YunDanDetailHeaderView *headerView = [[YunDanDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, height) takeOrderType:_takeOrderType Dict:_dataDic];
        headerView.vehicleArr = self.vehicleArr;
        headerView.delegate = self;
        if (_dataDic != nil) {
            headerView.infoDetailDic = _dataDic;
        }
        return headerView;
    } else {
        
        NSArray *arr = @[@[@"gcy_icon_order_huowu",@"货物信息"],@[@"gcy_icon_order_feiyong",@"费用信息"],@[@"gcy_icon_order_other",@"其他信息"]];
        
        YunDanDetailIconHeader *headerView = [[YunDanDetailIconHeader alloc] initWithFrame:CGRectMake(0, 0, ScreenW, headerHeightTwo)];
        headerView.iconInfoArray = arr[section-1];
        return headerView;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //    TouSuDetailViewController *tsdvc = [[TouSuDetailViewController alloc] init];
    //    TouSuListModel *model = self.dataArray[indexPath.row];
    //    tsdvc.touSuRecordsCode = model.code;
    //    [self.navigationController pushViewController:tsdvc animated:YES];
    

    if (indexPath.section == 2) {
        //点击费用信息区域
//        NSLog(@"%@, %ld", self.feiYongTitleArray[indexPath.row], indexPath.row);

        if ([self.feiYongTitleArray[indexPath.row] isEqual:@"其他费用"]) {
            
            YunDanCenterExtraFeeViewController *ydcevc = [[YunDanCenterExtraFeeViewController alloc] init];
            ydcevc.code = _orderCode;
            ydcevc.totalAmount = CJStringWithFormat(@"%@", _dataDic[@"orderDetail"][@"extraFee"]);
            [self.navigationController pushViewController:ydcevc animated:YES];
            
        } else if ([self.feiYongTitleArray[indexPath.row] isEqual:@"预付金额"]) {
            
            NSInteger currentIndex = indexPath.row;

            if (_isOpenYuFuAmount == NO) {
                                
                [self.feiYongTitleArray insertObject:@"预付现金" atIndex:currentIndex + 1];
                [self.feiYongTitleArray insertObject:@"预付油点" atIndex:currentIndex + 2];

                [self.feiYongValueArray replaceObjectAtIndex:currentIndex withObject:[NSString nullToString:CJStringWithFormat(@"%.2f元 V", [_dataDic[@"orderDetail"][@"payAmount"] floatValue])]];
                [self.feiYongValueArray insertObject:[NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"payAmount"] floatValue])] atIndex:currentIndex + 1];
                [self.feiYongValueArray insertObject:[NSString nullToString:CJStringWithFormat(@"%@",_dataDic[@"orderDetail"][@"payOil"])] atIndex:currentIndex + 2];
                
            } else {
                
                [self.feiYongValueArray replaceObjectAtIndex:currentIndex withObject:[NSString nullToString:CJStringWithFormat(@"%.2f元 >>", [_dataDic[@"orderDetail"][@"payAmount"] floatValue])]];
                
                [self.feiYongTitleArray removeObjectsInRange:NSMakeRange(currentIndex + 1, 2)];
                [self.feiYongValueArray removeObjectsInRange:NSMakeRange(currentIndex + 1, 2)];
   
            }
            
            _isOpenYuFuAmount = !_isOpenYuFuAmount;

            [self.tableView reloadData];
            
        } else if ([self.feiYongTitleArray[indexPath.row] isEqual:@"实收现金"]) {
            
            NSInteger currentIndex = indexPath.row;
            
            if (_isOpenShiShouCashAmount == NO) {
                
                [self.feiYongTitleArray insertObject:@"打款现金" atIndex:currentIndex + 1];
                [self.feiYongTitleArray insertObject:@"打款油点" atIndex:currentIndex + 2];

                [self.feiYongValueArray replaceObjectAtIndex:currentIndex withObject:[NSString nullToString:CJStringWithFormat(@"%.2f元 V", [_dataDic[@"orderDetail"][@"driverActualCash"] floatValue])]];
                [self.feiYongValueArray insertObject:[NSString nullToString:CJStringWithFormat(@"%.2f元", [_dataDic[@"orderDetail"][@"allotAmount"] floatValue])] atIndex:currentIndex + 1];
                [self.feiYongValueArray insertObject:[NSString nullToString:CJStringWithFormat(@"%@",_dataDic[@"orderDetail"][@"allotOil"])] atIndex:currentIndex + 2];
                
            } else {
                
                [self.feiYongValueArray replaceObjectAtIndex:currentIndex withObject:[NSString nullToString:CJStringWithFormat(@"%.2f元 >>", [_dataDic[@"orderDetail"][@"driverActualCash"] floatValue])]];
                
                [self.feiYongTitleArray removeObjectsInRange:NSMakeRange(currentIndex + 1, 2)];
                [self.feiYongValueArray removeObjectsInRange:NSMakeRange(currentIndex + 1, 2)];
   
            }
            
            _isOpenShiShouCashAmount = !_isOpenShiShouCashAmount;

            [self.tableView reloadData];
            
        }
        
    }
}
- (void)paidanAction{
    //车队可派单给车队司机（只能派给一个司机不能多选）
    PaiDanViewController *pdvc = [[PaiDanViewController alloc] init];
    pdvc.infoFee = _ydModel.infoFee;
    pdvc.driverIsShare = _ydModel.driverIsShare;
    pdvc.orderCode = _ydModel.code;
    pdvc.branchCode = _ydModel.branchCode;
    pdvc.status = 0;
    [self.navigationController pushViewController:pdvc animated:YES];
}
#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NavHeight);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YDCenterDetailCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
        
    }
    
    return _tableView;
}

- (NSMutableArray *)huoWuTitleArray {
    if (!_huoWuTitleArray) {
        _huoWuTitleArray = [[NSMutableArray alloc] initWithObjects:@"货物名称",@"货物单价",@"装车数量",@"卸车数量",@"合理损耗",@"运输距离",@"货物备注", nil];
    }
    return _huoWuTitleArray;
}
- (NSMutableArray *)huoWuValueArray {
    if (!_huoWuValueArray) {
        _huoWuValueArray = [[NSMutableArray alloc] init];
    }
    return _huoWuValueArray;
}

- (NSMutableArray *)feiYongTitleArray {
    if (!_feiYongTitleArray) {
//        _feiYongTitleArray = [[NSMutableArray alloc] initWithObjects:@"承运单价", nil];
        _feiYongTitleArray = [NSMutableArray array];
    }
    return _feiYongTitleArray;
}
- (NSMutableArray *)feiYongValueArray {
    if (!_feiYongValueArray) {
        _feiYongValueArray = [[NSMutableArray alloc] init];
    }
    return _feiYongValueArray;
}

- (NSMutableArray *)otherTitleArray {
    if (!_otherTitleArray) {
        _otherTitleArray = [[NSMutableArray alloc] initWithObjects:@"接单时间", @"装货时间", @"卸货时间", @"运输方式", @"运输要求", nil];
    }
    return _otherTitleArray;
}
- (NSMutableArray *)otherValueArray {
    if (!_otherValueArray) {
        _otherValueArray = [[NSMutableArray alloc] init];
    }
    return _otherValueArray;
}

@end
